<?
class ciXmlParser {
   var $array, $values, $tags, $stack, $counter;

   function read_file($file) {
     if (!(@ $fp = fopen("$file", "r")))
       error (ERROR_COULD_NOT_OPEN_XML.$file, '');
     while ($data = fread($fp, 4096))
       $file_contents .= $data;

     //echo (linebreak_view(echo_array($file_contents)));

     return $file_contents;
   }

   function parse($xml) {
     $xml_parser = xml_parser_create();
     xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
     xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 0);
     xml_parse_into_struct($xml_parser, $xml, $this->values, $this->tags);
      //echo (linebreak_view(echo_array($xml)));
     xml_parser_free($xml_parser);
   }

   function reformat_struct() {
     foreach ($this->values as $key => $current) {
 //       echo "Key: " . $key . "\n";
       switch ($current['type']) {
         case 'open':
           $this->stack[] = $current['tag'];

           $counter_array_ref =& $this->counter[$this->stack[0]];
           for($i = 1; $i < sizeof($this->stack); $i++) {
             $counter_array_ref =& $counter_array_ref[$this->stack[$i]];
           }
           $counter_array_ref[count]++;

           $array_ref =& $this->array[$this->stack[0]];
           $counter_array_ref =& $this->counter[$this->stack[0]];
           for($i = 1; $i < sizeof($this->stack); $i++) {
             if ($i != 1) {
               $array_ref =& $array_ref[($counter_array_ref['count']-1)];
             }
             $counter_array_ref =& $counter_array_ref[$this->stack[$i]];
             $array_ref =& $array_ref[$this->stack[$i]];
 //             if ($i != 1) {
               $array_ref =& $array_ref[($counter_array_ref['count']-1)];
 //             }
           }
 //           print_r($this->counter);
           $array_ref['@'] = $current['attributes'];
 /*
           $this->stack[] = $current['tag'];
           $array_ref =& $this->array[$this->stack[0]];

           $counter_array_ref =& $this->counter[$this->stack[0]];
           for($i = 1; $i < sizeof($this->stack); $i++) {
             $counter_array_ref =& $counter_array_ref[$this->stack[$i]];
           }
           $counter_array_ref[count]++;

           $counter_array_ref =& $this->counter[$this->stack[0]];
           for($i = 1; $i < sizeof($this->stack); $i++) {
             $counter_array_ref =& $counter_array_ref[$this->stack[$i]];
               if ($current['level'] != 1) {
                 $array_ref =& $array_ref[($counter_array_ref['count']-1)];
                 echo "OK\n";
               }
             $array_ref =& $array_ref[$this->stack[$i]];
           }
           $array_ref['@'] = $current['attributes'];
 */
           break;
         case 'cdata':
           break;
         case 'complete':
           $this->stack[] = $current['tag'];

           $counter_array_ref =& $this->counter[$this->stack[0]];
           for($i = 1; $i < sizeof($this->stack); $i++) {
             $counter_array_ref =& $counter_array_ref[$this->stack[$i]];
           }
           $counter_array_ref[count]++;

           $array_ref =& $this->array[$this->stack[0]];
           $counter_array_ref =& $this->counter[$this->stack[0]];
 //           print_r($this->stack);
           for($i = 1; $i < sizeof($this->stack); $i++) {
             if ($i != 1) {
               $array_ref =& $array_ref[($counter_array_ref['count']-1)];
             }
             $counter_array_ref =& $counter_array_ref[$this->stack[$i]];
             $array_ref =& $array_ref[$this->stack[$i]];
             if ($i != 1) {
               $array_ref =& $array_ref[($counter_array_ref['count']-1)];
             }
           }
 //           print_r($this->counter);
           $array_ref['@'] = $current['attributes'];
           $array_ref['#'] = $current['value'];
           array_pop($this->stack);
 /*
           $array_ref =& $this->array[$this->stack[0]];
           $counter_array_ref =& $this->counter[$this->stack[0]];
           for($i = 1; $i < sizeof($this->stack); $i++) {
             $counter_array_ref =& $counter_array_ref[$this->stack[$i]];
           }
           $counter_array_ref[count]++;
 //          $data = ltrim($data);
           for($i = 1; $i < sizeof($this->stack); $i++) {
 //              $array_ref =& $array_ref[];
             $counter_array_ref =& $counter_array_ref[$this->stack[$i]];
             if ($current['level'] != 1) {
               $array_ref =& $array_ref[($counter_array_ref['count']-1)];
               echo "OK\n";
             }
             $array_ref =& $array_ref[$this->stack[$i]];
           }
           $array_ref['#'] = $current['value'];
           $array_ref['@'] = $current['attributes'];
 */
           break;
         case 'close':
           // Resets everything in the counter array under the current level
           if ($current['level'] == 2) {
             $counter_array_ref =& $this->counter[$this->stack[0]];
             for($i = 1; $i < sizeof($this->stack); $i++) {
               $counter_array_ref =& $counter_array_ref[$this->stack[$i]];
             }
             foreach($counter_array_ref as $key=>$value) {
               if ($key != 'count') {
                 unset($counter_array_ref[$key]);
               }
             }
           }
           array_pop($this->stack);
           break;
       }
     }
   }

   function ciXmlParser($file) {
     $this->parse($this->read_file($file));
     $this->reformat_struct();
   }
 }





/*  Test print
$parser = new ciXmlParser($file);
 print_r($parser->array); //This is the array.
*/
?>
